package com.android.djlmapp.adapter.my;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.djlmapp.R;
import com.android.djlmapp.model.my.FeedBackInfo;
import com.android.djlmapp.tools.TimeUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Pattern;

public class FeedBackAdapter extends BaseAdapter {
    private ArrayList<FeedBackInfo.DataBean> bleNameList = new ArrayList<FeedBackInfo.DataBean>();
    private StringBuilder stringBuilder;
    private Activity mContext;
    MyHolder myHolder = null;

    public FeedBackAdapter(Activity mContext) {

        this.mContext = mContext;
    }

    public void setData(ArrayList<FeedBackInfo.DataBean> bean) {
        this.bleNameList = bean;
        notifyDataSetChanged();
    }

    public void add(List<FeedBackInfo.DataBean> msgs) {
        bleNameList.addAll(msgs);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return bleNameList.size() > 0 ? bleNameList.size() : 0;
    }

    @Override
    public FeedBackInfo.DataBean getItem(int position) {
        return bleNameList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * 清空所有数据
     */
    public void clear() {
        bleNameList.clear();
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            myHolder = new MyHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.feed_back_item, parent, false);

            myHolder.content = convertView.findViewById(R.id.fb_content);
            myHolder.time = convertView.findViewById(R.id.fb_time);
            myHolder.fb_resp = convertView.findViewById(R.id.fb_resp);
            myHolder.fb_line = convertView.findViewById(R.id.fb_line);
            myHolder.fb_resp_content = convertView.findViewById(R.id.fb_resp_content);
            myHolder.fb_resp_time = convertView.findViewById(R.id.fb_resp_time);
            convertView.setTag(myHolder);
        } else {
            myHolder = (MyHolder) convertView.getTag();
        }
        stringBuilder = new StringBuilder();
        String texColor = (position + 1) + ":问题反馈:";
        stringBuilder.append("T  "+texColor+"  ");
        stringBuilder.append(bleNameList.get(position).getContent());

        //带图片的文本
        SpannableString spannableStringImg=new SpannableString(stringBuilder.toString());

        Drawable drawable=mContext.getResources().getDrawable(R.mipmap.ques);
        drawable.setBounds(0,0,(int)mContext.getResources().getDimension(R.dimen.w_dp_25),(int)mContext.getResources().getDimension(R.dimen.w_dp_25));
        ImageSpan imageSpan=new ImageSpan(drawable);
        spannableStringImg.setSpan(imageSpan,0,1,Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

        ForegroundColorSpan foregroundColorSpanName=new ForegroundColorSpan(mContext.getResources().getColor(R.color.red_jk));//实例化ForegroundColorSpan对象并设置前景色
        spannableStringImg.setSpan(foregroundColorSpanName,3,texColor.length()+2, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);//设置字体颜色
        myHolder.content.setText(spannableStringImg);

        myHolder.time.setText(TimeUtils.timedate(bleNameList.get(position).getTime()));

        //回复
        if ("2".equals(bleNameList.get(position).getStatus())) {
            myHolder.fb_line.setVisibility(View.VISIBLE);
            Log.e("项目异常position", "-----(position + 1)------" + (position + 1));
            myHolder.fb_resp.setVisibility(View.VISIBLE);

            stringBuilder = new StringBuilder();
            texColor = (position + 1) + ":系统回复:";
            stringBuilder.append("T  "+texColor+"  ");
            stringBuilder.append(bleNameList.get(position).getResult());

            //带图片的文本
            spannableStringImg=new SpannableString(stringBuilder.toString());

            drawable=mContext.getResources().getDrawable(R.mipmap.resp);
            drawable.setBounds(0,0,(int)mContext.getResources().getDimension(R.dimen.w_dp_25),(int)mContext.getResources().getDimension(R.dimen.w_dp_25));
            imageSpan=new ImageSpan(drawable);
            spannableStringImg.setSpan(imageSpan,0,1,Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

            foregroundColorSpanName=new ForegroundColorSpan(mContext.getResources().getColor(R.color.jk_blue));//实例化ForegroundColorSpan对象并设置前景色
            spannableStringImg.setSpan(foregroundColorSpanName,3,texColor.length()+2, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);//设置字体颜色
            myHolder.fb_resp_content.setText(spannableStringImg);

            myHolder.fb_resp_time.setText(TimeUtils.timedate(bleNameList.get(position).getTime()));
        }else{
            myHolder.fb_line.setVisibility(View.GONE);
            myHolder.fb_resp.setVisibility(View.GONE);
        }

        return convertView;
    }

    static class MyHolder {
        TextView content;
        TextView time;
        RelativeLayout fb_resp;
        TextView fb_line;
        TextView fb_resp_content;
        TextView fb_resp_time;
    }
}
